<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>登录</title>
    <style>
        * {
            margin: 0px;
            padding: 0px;
            border: 0px;
            list-style: none;
        }

        input {
            border: 1px solid #f7f7f7;
            height: 30px;
            line-height: 30px;
            width: 200px;
        }

        .login {
            position: absolute;
            width: 400px;
            height: 300px;
            border: 1px solid red;
            left: calc(50% - 200px);
            top: calc(50% - 150px);
        }

        .login_btn {
            width: 100px;
            height: 30px;
            background: green;
            border: #ada;
            text-align: center;
            line-height: 30px;
            color: #fff;
            border-radius: 3px;
            margin: 50px auto;
            cursor: pointer;
        }

        .login_btn:hover {
            background: orangered;
        }
    </style>
</head>

<body>
    <div class="login">
        <div id="login_result_msg" style="color:red">

        </div>
        <div style="margin:10px">
            <label for="username">用户名：</label>
            <input type="text" id="username" />
            <label id="username_msg" style="color:red"></label>
        </div>
        <div style="margin:10px">
            <label for="password">&nbsp;&nbsp;&nbsp;&nbsp;密码：</label>
            <input type="password" id="password" />
            <label id="password_msg" style="color:red"></label>
        </div>
        <div>
            <div class="login_btn" id="login_btn">登录</div>
        </div>
    </div>


    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script>


        $(function () {

            function login() {
                $('#username_msg').html('');
                $('#password_msg').html('');

                if ($.trim($("#username").val()) == '') {
                    $('#username_msg').html('请输入账户名');
                    return false;
                }

                if ($.trim($("#password").val()) == '') {
                    $('#password_msg').html('请输入密码');
                    return false;
                }

                $.ajax({
                    url: '/user/login',
                    cache:false,
                    type:'post',
                    data: { username: $("#username").val(), pwd: $("#password").val() },
                    success: function (data) {
                        if (data && data.result == 1) {
                            location.href = 'index.html'
                        } else {
                            $("#login_result_msg").html("用户名或密码错误！");
                        }
                    }
                })
            }

            $(window).keydown(function (e) {
                if (e.keyCode == 13) {
                    login()
                }
            })

            $("#login_btn").click(function () {
                login()
            });
        })
    </script>

</body>

</html>